#include "StdAfx.h"
#include "LObj.h"

//static int mobjcnt =0;
list<LObj*> LObj::mexitlist;
//int LObj::mtest =0;
//vector<int> LObj::mtestvec;
LTest LObj::mltest;

LObj::LObj(void)
{
	//lnewborn( mobjcnt );

	mstate = 0;
	mlismustinit = false;
	//minitcnt = 0;
	mloopcnt =0;
	mexitlpent = 0;

	mclassname = "lobj";
}


LObj::~LObj(void)
{
	// quit from exitlist;
	lquitExitList( this);

	// check linit,lexit must be called;
	if( mlismustinit )
		lckState(mstate, 2 );
	else
		lckStateif(mstate, 2);

	// destory;
	//ldestory();
}

int LObj::linit( int state)
{
	if( mloopcnt > 0)	// already enter;
		return 0;
	else
		lincLoopcnt();

	//lincInitcnt();

	int ret = 0;
	if( lckStateMulti( mstate, state )) 
	{
		//ldecLoopcnt();
		ret = 1;
	}
	else
	{
		TRACE("=> %s init enter.\n", mclassname.c_str());
		ret = beforeAutoinit();
		ret = linitCreate();
		ret = init();
		
		laddExitList(this);
		TRACE("=> %s init exit.\n", mclassname.c_str());
	}
	
	ldecLoopcnt();
	
	return ret;
}
void LObj::lexit( int state )
{
	if( mexitlpent > 0 )
		return ;
	else
		lincExitlpent();

	//ldecInitcnt();

	int ret = 0;
	//if( minitcnt >0 ) 
	//{
		//lautoExitobj();
	//}
	//else 
	if (lckStateMulti(mstate, state )) 
	{
		
	}
	else
	{
		TRACE("=> %s exit enter.\n", mclassname.c_str());
		exit();

		//lautoExitobj();

		//afterAutoExit();
		TRACE("=> %s exit eixt.\n", mclassname.c_str());
	}
	
	ldecExitlpent();

}
void LObj::laddExitList( LObj * obj )
{
	mexitlist.push_back(obj);
}
void LObj::lexitAll()
{
	LObj * obj = NULL;
	list<LObj*>::reverse_iterator rit;
	if( mexitlist.size() > 0)
	{
		for( rit= mexitlist.rbegin(); rit!=mexitlist.rend(); rit++)
		{
			obj = *rit;
			//mexitlist.erase( rit);
			VERIFY(obj);
			obj->lexit();
		}
		//mexitlist.clear();
	}
	//mtest= mexitlist.size();
	//mtestvec.push_back( 1 );
	//if( mexitlist.size() > 0)
	//	mexitlist.clear();
	/*
	LObj * obj = NULL;
	int size = mexitlist.size();
	for( int i=size-1; i>=0; i--)
	{
		obj = mexitlist.at(i);
		VERIFY(obj);
		obj->lexit();
	}
	if( size > 0 )
		mexitlist.clear();
		*/
}

void LObj::lquitExitList( LObj * qobj )
{
	LObj * obj=NULL;
	list<LObj*>::iterator it;
	int i;//= mtest;
	//i = mtestvec.size();
	i = mltest.mtest;
	if( mexitlist.size() > 0)
	{
		for (it = mexitlist.begin(); it != mexitlist.end(); it++)
		{
			obj = *it;
			if( qobj == obj )
			{
				mexitlist.erase( it );
				return;
			}
		}
	}
	
	/*
	int size = mexitlist.size();
	//for ( int i= size-1; i>=0; i--)
	for( int i=0; i < size; i++)
	{
		//obj = minitlist.at( i ); 
		if( obj == qobj )
		{
			//mexitlist.erase( i );
			return;
		}
	}
	*/
}
/*
void LObj::lautoExitobj()
{
	LObj * obj=NULL;
	int size = minitlist.size();
	//for ( int i= size-1; i>=0; i--)
	for( int i=0; i < size; i++)
	{
		obj = minitlist.at( i ); 
		VERIFY( obj );
		obj->lexit();
		//TRACE(TEXT("->lobj.autoExitobj =%d\n"),i);
	}
	if( size >0 )
		minitlist.clear();
}
*/

void LObj::laddCreate( LObj * obj )
{
	mcreatlist.push_back( obj );
}
void LObj::ldestory()
{
	destory();				// 140503;

	LObj *obj = NULL;
	list<LObj *>::reverse_iterator rit;
	if( mcreatlist.size() > 0 )
	{
		for( rit= mcreatlist.rbegin(); rit != mcreatlist.rend(); rit++)
		{
			obj = *rit;
			if( obj )
			{
				delete obj;
			}
		}
		mcreatlist.clear();
	}
	
}
int LObj::linitCreate()
{
	int ret =0;
	LObj * obj = NULL;
	list<LObj*>::iterator it;
	for( it= mcreatlist.begin(); it != mcreatlist.end(); it++)
	{
		obj = *it;
		if( ret = lautoInitobj( obj ) < 0 )
			break;;
	}
	return ret;
}
//--------------------------------------------
void LObj::setObj( void * obj, int idx )
{

}


LTest::LTest()
{
	TRACE("test enter");
	mtest = 1;
}
LTest::~LTest()
{
	TRACE("test exit");
	mtest = 0;
}